Imports Microsoft.VisualBasic
Imports SQLtoASPX

Public Enum SearchMethod
    None = 1
    Boolean2 = 2
    Boolean3 = 3
    LeftOrRight = 4
    LeftOnly = 5
    RightOnly = 6
    ExactMatch = 7
End Enum

Public Class PCGColumn


    Public Search As String

    Private mDataFormat As String
    Public Property DataFormat() As String
        Get
            Return mDataFormat
        End Get
        Set(ByVal value As String)
            mDataFormat = value
        End Set
    End Property


    Private mLinkTableName As String
    Public Property LinkTableName() As String
        Get
            Return mLinkTableName
        End Get
        Set(ByVal value As String)
            mLinkTableName = value
        End Set
    End Property

    Public ReadOnly Property LinkTableAlias() As String
        Get
            Return LinkTableName & ColumnName
        End Get

    End Property


    Private mLinkTableKey As String
    Public Property LinkTableKey() As String
        Get
            Return mLinkTableKey
        End Get
        Set(ByVal value As String)
            mLinkTableKey = value
        End Set
    End Property

    Private mLinkTableValue As String
    Public Property LinkTableValue() As String
        Get
            Return mLinkTableValue
        End Get
        Set(ByVal value As String)
            mLinkTableValue = value
        End Set
    End Property

    Public ReadOnly Property IsDate() As Boolean
        Get
            Select Case mDataType.ToUpper
                Case "DATETIME", "SMALLDATETIME", "TIMESTAMP"
                    Return True
            End Select
            Return False
        End Get
    End Property

    Public ReadOnly Property IsCheckBoxColumn() As Boolean
        Get
            Select Case DataType.ToUpper
                Case "BIT"
                    Return True
                Case Else
                    Return False
            End Select
        End Get
    End Property

    Private mDisplayInGrid As Boolean
    Public Property DisplayInGrid() As Boolean
        Get
            Return mDisplayInGrid
        End Get
        Set(ByVal value As Boolean)
            mDisplayInGrid = value
        End Set
    End Property

    Private mDisplayReadOnly As Boolean
    Public Property DisplayReadOnly() As Boolean
        Get
            Return mDisplayReadOnly
        End Get
        Set(ByVal value As Boolean)
            mDisplayReadOnly = value
        End Set
    End Property

    Private mIsDisplayColumn As Boolean
    Public Property IsDisplayColumn() As Boolean
        Get
            Return mIsDisplayColumn
        End Get
        Set(ByVal value As Boolean)
            mIsDisplayColumn = value
        End Set
    End Property

    Private mColumnName As String


    Public ReadOnly Property IsAutoSync() As Boolean
        Get
            If DataType.ToUpper = "AUTONUMBER" Then
                Return True
            End If
        End Get
    End Property

    Public ReadOnly Property IsDbGenerated() As Boolean
        Get
            If DataType.ToUpper = "AUTONUMBER" Then
                Return True
            End If
        End Get
    End Property

    Public Property ColumnName() As String
        Get
            Return mColumnName
        End Get
        Set(ByVal value As String)
            mColumnName = value
        End Set
    End Property

    Public ReadOnly Property BoundField() As String
        Get
            If LinkTableName.Length > 0 And LinkTableKey.Length > 0 And LinkTableValue.Length > 0 Then
                Return LinkTableName & ColumnName & LinkTableValue
            Else
                Return mColumnName
            End If

        End Get
    End Property


    Public ReadOnly Property SortBoundField(ByVal TableGen As RawCreateScript) As String
        Get
            If LinkTableName.Length > 0 And LinkTableKey.Length > 0 And LinkTableValue.Length > 0 Then
                Return LinkTableAlias & "." & LinkTableValue
            Else
                Return TableGen.TableName & "." & mColumnName
            End If

        End Get
    End Property

    Private mDataType As String

    Public Property DataType() As String
        Get
            Return mDataType
        End Get
        Set(ByVal value As String)
            mDataType = value
        End Set
    End Property

    Public ReadOnly Property AccessCreateColumnSQL() As String
        Get
            Dim SQL As String
            Select Case mDataType.ToUpper
                Case "AUTONUMBER"

                    Return "AUTOINCREMENT"
                Case "UNIQUEIDENTIFIER", "VARBINARY"

                    SQL = "GUID"

                Case "NCHAR", "NVARCHAR", "NTEXT", "TEXT", "CHAR", "VARCHAR"
                    If Me.Length > 255 Then
                        SQL = "MEMO"
                    Else
                        SQL = "TEXT"
                        If Me.Length > 0 Then
                            SQL = SQL & " (" & Me.Length & ") "
                        End If

                    End If
                Case "BIT"
                    SQL = "YesNo"
                Case "BIGINT"
                    SQL = "LONG"

                Case "INT"
                    SQL = "LONG"

                Case "SMALLINT", "TINYINT"
                    SQL = "INTEGER"

                Case "FLOAT", "REAL"
                    SQL = "DOUBLE"


                Case "NUMERIC", "DECIMAL"
                    SQL = "DECIMAL"


                Case "MONEY", "SMALLMONEY"
                    SQL = "MONEY"

                Case "DATETIME", "TIMESTAMP", "SMALLDATETIME"
                    SQL = "DATETIME"

                Case Else
                    SQL = Me.DataType
                    If Me.Length > 0 Then
                        SQL = SQL & " (" & Me.Length & ") "
                    End If

            End Select
            If Me.AllowNulls Then
                SQL = SQL & " NULL"
            Else
                SQL = SQL & " NOT NULL"
            End If

            If Me.DefaultValue.Length > 0 Then
                SQL = SQL & " DEFAULT " & Me.DefaultValue
            End If

            Return SQL
        End Get
    End Property

    Private mAllowNulls As Boolean
    Public Property AllowNulls() As Boolean
        Get
            Return mAllowNulls
        End Get
        Set(ByVal value As Boolean)
            mAllowNulls = value
        End Set
    End Property

    Private mScale As Integer
    Public Property Scale() As Integer
        Get
            Return mScale
        End Get
        Set(ByVal value As Integer)
            mScale = value
        End Set
    End Property

    Private mPrecision As Integer
    Public Property Precision() As Integer
        Get
            Return mPrecision
        End Get
        Set(ByVal value As Integer)
            mPrecision = value
        End Set
    End Property

    Private mLength As String
    Public Property Length() As String
        Get
            Return mLength
        End Get
        Set(ByVal value As String)
            mLength = value
        End Set
    End Property

    Private mDefaultValue As String
    Public Property DefaultValue() As String
        Get
            Return mDefaultValue
        End Get
        Set(ByVal value As String)
            mDefaultValue = value
        End Set
    End Property

    Private mIsPrimaryKey As Boolean
    Public Property IsPrimaryKey() As Boolean
        Get
            Return mIsPrimaryKey
        End Get
        Set(ByVal value As Boolean)
            mIsPrimaryKey = value
        End Set
    End Property

    Private mIsIdentity As Boolean
    Public Property IsIdentity() As Boolean
        Get
            Return mIsIdentity
        End Get
        Set(ByVal value As Boolean)
            mIsIdentity = value
        End Set
    End Property

    Public Sub New()
        mIsPrimaryKey = False
    End Sub

    Public Sub New(ByVal sql As String)
        mIsPrimaryKey = False
        mDefaultValue = ""
        mColumnName = ""
        mDataType = ""
        sql = Trim(sql)
        Dim LiteralNotFound As Boolean = False
        Dim LiteralNullFound As Boolean = False
        If Left(sql, 1) = "[" Then
            Dim SecondBrace As Integer = InStr(1, sql, "]")
            If SecondBrace > 0 Then
                mColumnName = Mid(sql, 2, SecondBrace - 2)
                sql = Right(sql, Len(sql) - Len(mColumnName) - 2)
            Else
                Throw New Exception("Parse Error - No Closing ']'")
            End If
        Else
            Dim NextSpace As Integer = InStr(1, sql, " ", CompareMethod.Text)
            If NextSpace > 0 Then
                mColumnName = Left(sql, NextSpace)
            Else
                Throw New Exception("Parse Error - No Closing ' '")
            End If
        End If
        Dim array As ArrayList = Support.SplitList(sql, "[] ")
        array.Insert(0, mColumnName)
        If array.Count > 0 Then
            ColumnName = array(0)
        End If
        If array.Count > 1 Then
            Select Case array(1).ToString.ToUpper
                Case "INT"
                    DataType = "INT"
                    For i As Integer = 2 To array.Count - 1
                        If InStr(array(i).ToString, "IDENTITY") > 0 Then
                            IsIdentity = True
                            DataType = "AUTONUMBER"
                        End If
                    Next
                Case "DATETIME"
                    DataType = "DATETIME"
                Case "VARCHAR"
                    DataType = "VARCHAR"
                    If array.Count > 2 Then
                        If array(2).ToString.Trim(")", "(").ToUpper = "MAX" Then
                            Length = 8000
                        Else
                            Length = array(2).ToString.Trim(")", "(")
                        End If

                    End If
                Case "NCHAR"
                    DataType = "NCHAR"
                    If array.Count > 2 Then
                        Length = array(2).ToString.Trim(")", "(")
                    End If
                Case "NVARCHAR"
                    DataType = "NVARCHAR"
                    If array.Count > 2 Then
                        Length = array(2).ToString.Trim(")", "(")
                    End If
                Case "NTEXT"
                    DataType = "NTEXT"
                    If array.Count > 2 Then
                        ' Length = Array(2).ToString.Trim(")", "(")
                    End If
                Case "TEXT"
                    DataType = "TEXT"
                    If array.Count > 2 Then
                        'Length = Array(2).ToString.Trim(")", "(")
                    End If
                Case "CHAR"
                    DataType = "CHAR"
                    If array.Count > 2 Then
                        Length = array(2).ToString.Trim(")", "(")
                    End If
                Case "BIT"
                    DataType = "BIT"
                Case "NUMERIC"
                    DataType = "NUMERIC"
                Case Else
                    DataType = array(1).ToString.ToUpper
            End Select
            If InStr(sql.ToUpper, "NOT NULL", CompareMethod.Text) > 0 Then
                AllowNulls = False
            Else
                AllowNulls = True
            End If

            Dim DefaultLocation As Integer = InStr(sql, " DEFAULT ", CompareMethod.Text)
            If DefaultLocation > 0 Then
                Dim DefaultText = Right(sql, Len(sql) - DefaultLocation - 8)
                Dim Result = ParenProcess(DefaultText)
                mDefaultValue = Result
            End If
        End If
    End Sub

    Public Function ParenProcess(ByVal s As String) As String
        '(getdate()), 
        ParenProcess = ""
        Dim Level As Integer = 0

        For Each c As Char In s
            If c = "(" Then
                Level = Level + 1
            End If

            If c = ")" Then
                If Level = 1 Then
                    Exit Function
                End If
                Level = Level - 1
            End If

            If c = "(" And Level = 1 Then
            Else
                ParenProcess = ParenProcess & c
            End If

        Next
    End Function



    Public Function SQLSelectSyntax() As String
        Select Case DataType.ToUpper
            Case "INT", "AUTONUMBER", "DECIMAL", "FLOAT", "INT", "REAL", "SMALLINT", "TINYINT", "MONEY", "SMALLMONEY", "NUMERIC"
                SQLSelectSyntax = "' +  ISNULL(cast(" & ColumnName & " as varchar ),'NULL') + '"
            Case "DATETIME", "TIMESTAMP", "SMALLDATETIME"
                SQLSelectSyntax = "'+ ISNULL('''' + cast(" & ColumnName & " as varchar ) + '''','NULL') + '"
            Case "VARCHAR", "NCHAR", "NVARCHAR", "NTEXT", "TEXT", "CHAR", "UNIQUEIDENTIFIER", "VARBINARY"
                SQLSelectSyntax = "'+ ISNULL('''' + cast(" & ColumnName & " as varchar ) + '''','NULL') + '"
            Case "BIT"
                SQLSelectSyntax = "' + ISNULL(cast(" & ColumnName & " as varchar ),'NULL') + '"
            Case "NUMERIC"
                SQLSelectSyntax = "' + ISNULL(" & ColumnName & ",'NULL') + '"
            Case Else
                SQLSelectSyntax = "' + ISNULL(" & ColumnName & ",'NULL') + '"
        End Select
        SQLSelectSyntax = SQLSelectSyntax & vbCrLf
    End Function

    ''' <summary>
    ''' testsss
    ''' summmarie
    ''' </summary>
    ''' <returns>ret</returns>
    ''' <remarks>rem</remarks>
    Public Function ControlName(Optional ByVal Prefix As String = "") As String
        ControlName = ""
        If DisplayReadOnly = True Then
            ControlName = "lbl"
        Else
            Select Case DataType.ToUpper
                Case "BIT"
                    ControlName = "chk"
                Case Else
                    ControlName = "txt"
            End Select
        End If

        ControlName = ControlName & Prefix & SafeParameterName()
    End Function

    Public Function ControlNameSearch(Optional ByVal Prefix As String = "Search") As String
        ControlNameSearch = ""
        If LinkTableName.Length > 0 And LinkTableKey.Length > 0 And LinkTableValue.Length > 0 Then
            ControlNameSearch = "cbo"
        Else
            Select Case DataType.ToUpper
                Case "BIT"
                    ControlNameSearch = "cbo"
                Case Else
                    ControlNameSearch = "txt"
            End Select
        End If

        ControlNameSearch = ControlNameSearch & Prefix & SafeParameterName()
    End Function

    Public Shared Function StaticSafeParameterName(ByVal ColumnName As String, Optional ByVal Prefix As String = "") As String
        StaticSafeParameterName = ""
        Dim Chars() As Char = ColumnName.Trim.ToCharArray()
        Dim Count As Integer = 0
        For Each c As Char In Chars
            Select Case c
                Case "."
                    StaticSafeParameterName = StaticSafeParameterName & "Dot"
                Case " "
                    StaticSafeParameterName = StaticSafeParameterName & "_"
                Case "+"
                    StaticSafeParameterName = StaticSafeParameterName & "Plus"
                Case "-"
                    StaticSafeParameterName = StaticSafeParameterName & "Minus"
                Case "?"
                    StaticSafeParameterName = StaticSafeParameterName & "Question"
                Case "~"
                    StaticSafeParameterName = StaticSafeParameterName & "Tilde"
                Case "`"
                    StaticSafeParameterName = StaticSafeParameterName & "BackTick"
                Case "'"
                    StaticSafeParameterName = StaticSafeParameterName & "Tick"
                Case "!"
                    StaticSafeParameterName = StaticSafeParameterName & "Exclamation"
                Case "@"
                    StaticSafeParameterName = StaticSafeParameterName & "At"
                Case "#"
                    StaticSafeParameterName = StaticSafeParameterName & "Pound"
                Case "$"
                    StaticSafeParameterName = StaticSafeParameterName & "Dollar"
                Case "%"
                    StaticSafeParameterName = StaticSafeParameterName & "Percent"
                Case "^"
                    StaticSafeParameterName = StaticSafeParameterName & "Caret"
                Case "&"
                    StaticSafeParameterName = StaticSafeParameterName & "And"
                Case "*"
                    StaticSafeParameterName = StaticSafeParameterName & "Asterisk"
                Case "("
                    StaticSafeParameterName = StaticSafeParameterName & "LeftParen"
                Case ")"
                    StaticSafeParameterName = StaticSafeParameterName & "RightParen"
                Case "_"
                    StaticSafeParameterName = StaticSafeParameterName & "UnderScore"
                Case "="
                    StaticSafeParameterName = StaticSafeParameterName & "Equals"
                Case "["
                    StaticSafeParameterName = StaticSafeParameterName & "RightBracket"
                Case "]"
                    StaticSafeParameterName = StaticSafeParameterName & "LeftBracket"
                Case "{"
                    StaticSafeParameterName = StaticSafeParameterName & "RightBrace"
                Case "}"
                    StaticSafeParameterName = StaticSafeParameterName & "LeftBrace"
                Case "|"
                    StaticSafeParameterName = StaticSafeParameterName & "Pipe"
                Case "\"
                    StaticSafeParameterName = StaticSafeParameterName & "BackSlash"
                Case "/"
                    StaticSafeParameterName = StaticSafeParameterName & "ForwardSlash"
                Case "<"
                    StaticSafeParameterName = StaticSafeParameterName & "LessThan"
                Case ">"
                    StaticSafeParameterName = StaticSafeParameterName & "GreaterThan"
                Case ","
                    StaticSafeParameterName = StaticSafeParameterName & "Comma"
                Case ":"
                    StaticSafeParameterName = StaticSafeParameterName & "Colon"
                Case ";"
                    StaticSafeParameterName = StaticSafeParameterName & "SemiColon"
                Case """"
                    StaticSafeParameterName = StaticSafeParameterName & "DoubleQuote"
                Case Else
                    If Count = 0 Then
                        If InStr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", c.ToString, CompareMethod.Text) > 0 Then
                            StaticSafeParameterName = StaticSafeParameterName & c.ToString
                        Else
                            StaticSafeParameterName = StaticSafeParameterName & "_"
                        End If
                    Else
                        If InStr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c.ToString, CompareMethod.Text) > 0 Then
                            StaticSafeParameterName = StaticSafeParameterName & c.ToString
                        Else
                            StaticSafeParameterName = StaticSafeParameterName & "_"
                        End If
                    End If

            End Select
            Count = Count + 1
        Next
        Return Prefix & StaticSafeParameterName
        'Return Replace(Replace(ColumnName.Trim, ".", "_"), " ", "_")
    End Function


    Public Function SafeParameterName(Optional ByVal Prefix As String = "") As String
        SafeParameterName = ""
        Dim Chars() As Char = ColumnName.Trim.ToCharArray()
        Dim Count As Integer = 0
        For Each c As Char In Chars
            Select Case c
                Case "."
                    SafeParameterName = SafeParameterName & "Dot"
                Case " "
                    SafeParameterName = SafeParameterName & "_"
                Case "+"
                    SafeParameterName = SafeParameterName & "Plus"
                Case "-"
                    SafeParameterName = SafeParameterName & "Minus"
                Case "?"
                    SafeParameterName = SafeParameterName & "Question"
                Case "~"
                    SafeParameterName = SafeParameterName & "Tilde"
                Case "`"
                    SafeParameterName = SafeParameterName & "BackTick"
                Case "'"
                    SafeParameterName = SafeParameterName & "Tick"
                Case "!"
                    SafeParameterName = SafeParameterName & "Exclamation"
                Case "@"
                    SafeParameterName = SafeParameterName & "At"
                Case "#"
                    SafeParameterName = SafeParameterName & "Pound"
                Case "$"
                    SafeParameterName = SafeParameterName & "Dollar"
                Case "%"
                    SafeParameterName = SafeParameterName & "Percent"
                Case "^"
                    SafeParameterName = SafeParameterName & "Caret"
                Case "&"
                    SafeParameterName = SafeParameterName & "And"
                Case "*"
                    SafeParameterName = SafeParameterName & "Asterisk"
                Case "("
                    SafeParameterName = SafeParameterName & "LeftParen"
                Case ")"
                    SafeParameterName = SafeParameterName & "RightParen"
                Case "_"
                    SafeParameterName = SafeParameterName & "UnderScore"
                Case "="
                    SafeParameterName = SafeParameterName & "Equals"
                Case "["
                    SafeParameterName = SafeParameterName & "RightBracket"
                Case "]"
                    SafeParameterName = SafeParameterName & "LeftBracket"
                Case "{"
                    SafeParameterName = SafeParameterName & "RightBrace"
                Case "}"
                    SafeParameterName = SafeParameterName & "LeftBrace"
                Case "|"
                    SafeParameterName = SafeParameterName & "Pipe"
                Case "\"
                    SafeParameterName = SafeParameterName & "BackSlash"
                Case "/"
                    SafeParameterName = SafeParameterName & "ForwardSlash"
                Case "<"
                    SafeParameterName = SafeParameterName & "LessThan"
                Case ">"
                    SafeParameterName = SafeParameterName & "GreaterThan"
                Case ","
                    SafeParameterName = SafeParameterName & "Comma"
                Case ":"
                    SafeParameterName = SafeParameterName & "Colon"
                Case ";"
                    SafeParameterName = SafeParameterName & "SemiColon"
                Case """"
                    SafeParameterName = SafeParameterName & "DoubleQuote"
                Case Else
                    If Count = 0 Then
                        If InStr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", c.ToString, CompareMethod.Text) > 0 Then
                            SafeParameterName = SafeParameterName & c.ToString
                        Else
                            SafeParameterName = SafeParameterName & "_"
                        End If
                    Else
                        If InStr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c.ToString, CompareMethod.Text) > 0 Then
                            SafeParameterName = SafeParameterName & c.ToString
                        Else
                            SafeParameterName = SafeParameterName & "_"
                        End If
                    End If

            End Select
            Count = Count + 1
        Next
        Return Prefix & SafeParameterName
        'Return Replace(Replace(ColumnName.Trim, ".", "_"), " ", "_")
    End Function

    Public Shared Function SafeParameterNameDotSpaceToUnderScore(ByVal ColumnName As String) As String
        Return Replace(Replace(ColumnName.Trim, ".", "_"), " ", "_")
    End Function


    Public Function ControlTypeNameSearch() As String
        ControlTypeNameSearch = ""

        If LinkTableName.Length > 0 And LinkTableKey.Length > 0 And LinkTableValue.Length > 0 Then
            ControlTypeNameSearch = "asp:DropDownList"
        Else

            Select Case DataType.ToUpper
                Case "BIT"
                    ControlTypeNameSearch = "asp:CheckBox"
                Case Else
                    ControlTypeNameSearch = "asp:TextBox"
            End Select
        End If
    End Function


    Public Function ControlTypeName() As String
        ControlTypeName = ""
        If DisplayReadOnly Then
            ControlTypeName = "asp:Label"
        Else
            Select Case DataType.ToUpper
                Case "BIT"
                    ControlTypeName = "asp:CheckBox"
                Case Else
                    ControlTypeName = "asp:TextBox"
            End Select
        End If
    End Function

    Public Function ControlType(ByVal TargetCode As String) As String
        ControlType = ""

        Select Case DisplayReadOnly
            Case True
                Select Case TargetCode
                    Case CodeTypeEnum.VB2005
                        ControlType = "System.Web.UI.WebControls.Label"
                End Select
            Case False
                Select Case TargetCode
                    Case CodeTypeEnum.VB2005
                        Select Case DataType.ToUpper
                            Case "BIT"
                                ControlType = "System.Web.UI.WebControls.CheckBox"
                            Case Else
                                ControlType = "System.Web.UI.WebControls.TextBox"
                        End Select
                End Select
        End Select
    End Function

    Public Function ControlAccessor() As String
        ControlAccessor = ""
        If LinkTableName.Length > 0 And LinkTableKey.Length > 0 And LinkTableValue.Length > 0 Then
            ControlAccessor = "SelectedValue"
        Else
            If DisplayReadOnly = True Then
                ControlAccessor = "Text"
            Else
                Select Case DataType.ToUpper
                    Case "BIT"
                        ControlAccessor = "Checked"
                    Case Else
                        ControlAccessor = "Text"
                End Select
            End If
        End If

    End Function

    Public Function ControlAccessorSearch() As String
        ControlAccessorSearch = ""
        If LinkTableName.Length > 0 And LinkTableKey.Length > 0 And LinkTableValue.Length > 0 Then
            ControlAccessorSearch = "SelectedValue"
        Else

            If DisplayReadOnly = True Then
                ControlAccessorSearch = "Text"
            Else
                Select Case DataType.ToUpper
                    Case "BIT"
                        ControlAccessorSearch = "SelectedValue"
                    Case Else
                        ControlAccessorSearch = "Text"
                End Select
            End If
        End If
    End Function


    Public Function ColumnNameDescriptive() As String
        ColumnNameDescriptive = ""
        If UCase(ColumnName) = ColumnName Then
            ColumnNameDescriptive = ColumnName
        Else
            Dim FirstChar As Boolean = True
            For Each c As Char In ColumnName
                If FirstChar Then
                    ColumnNameDescriptive = c
                    FirstChar = False
                Else
                    If Char.IsUpper(c) Then
                        ColumnNameDescriptive = ColumnNameDescriptive & " " & c
                    Else
                        ColumnNameDescriptive = ColumnNameDescriptive & c
                    End If
                End If
            Next
        End If
        ColumnNameDescriptive = Replace(ColumnNameDescriptive, " I D", " ID", , , CompareMethod.Text)
    End Function

    Public ReadOnly Property DBMLDataComparison() As String
        '    If (Me._xBigInt.Equals(value) = False) Then
        '    If (Object.Equals(Me._xBinary1, value) = False) Then
        '    If (Object.Equals(Me._xBinary50, value) = False) Then
        '    If (Object.Equals(Me._xImage, value) = False) Then
        '    If (Object.Equals(Me._xsqlvariant, value) = False) Then
        '    If (Object.Equals(Me._xxml, value) = False) Then
        Get
            Select Case mDataType.ToUpper
                Case "AUTONUMBER"
                    Return "((Me._" & Me.ColumnName() & " = value) = False)"
                Case "BIT"
                    Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                Case "UNIQUEIDENTIFIER"
                    Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                Case "VARBINARY"
                    Return "(Object.Equals(Me._" & Me.ColumnName() & ", value) = False)"
                Case "NCHAR"
                    If Me.Length = 1 Then
                        Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                    Else
                        Return "(String.Equals(Me._" & Me.ColumnName() & ", value) = False)"
                    End If
                Case "NVARCHAR"
                    If Me.Length = 1 Then
                        Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                    Else
                        Return "(String.Equals(Me._" & Me.ColumnName() & ", value) = False)"
                    End If
                Case "NTEXT"
                    Return "(String.Equals(Me._" & Me.ColumnName() & ", value) = False)"
                Case "TEXT"
                    Return "(String.Equals(Me._" & Me.ColumnName() & ", value) = False)"
                Case "CHAR"
                    If Me.Length = 1 Then
                        Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                    Else
                        Return "(String.Equals(Me._" & Me.ColumnName() & ", value) = False)"
                    End If
                Case "VARCHAR"
                    If Me.Length = 1 Then
                        Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                    Else
                        Return "(String.Equals(Me._" & Me.ColumnName() & ", value) = False)"
                    End If
                Case "BIGINT"
                    Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                Case "INT"
                    Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                Case "SMALLINT"
                    Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                Case "TINYINT"
                    Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                Case "FLOAT"
                    Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                Case "REAL"
                    Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                Case "NUMERIC"
                    Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                Case "DECIMAL"
                    Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                Case "MONEY"
                    Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                Case "SMALLMONEY"
                    Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                Case "DATETIME"
                    Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                Case "TIMESTAMP"
                    Return "(Object.Equals(Me._" & Me.ColumnName() & ", value) = False)"
                Case "SMALLDATETIME"
                    Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
                Case Else
                    Return "(Me._" & Me.ColumnName() & ".Equals(value) = False)"
            End Select
        End Get
    End Property

    Public ReadOnly Property DBMLDataClassName() As String
        Get
            Select Case mDataType.ToUpper
                Case "AUTONUMBER"
                    Return "Integer"
                Case "BIT"
                    Return "Boolean"
                Case "UNIQUEIDENTIFIER"
                    Return "System.Guid"
                Case "VARBINARY"
                    Return "System.Data.Linq.Binary"
                Case "NCHAR"
                    If Me.Length = 1 Then
                        Return "Char"
                    Else
                        Return "String"
                    End If
                Case "NVARCHAR"
                    If Me.Length = 1 Then
                        Return "Char"
                    Else
                        Return "String"
                    End If
                Case "NTEXT"
                    Return "String"
                Case "TEXT"
                    Return "String"
                Case "CHAR"
                    If Me.Length = 1 Then
                        Return "Char"
                    Else
                        Return "String"
                    End If
                Case "VARCHAR"
                    If Me.Length = 1 Then
                        Return "Char"
                    Else
                        Return "String"
                    End If
                Case "BIGINT"
                    Return "Long"
                Case "INT"
                    Return "Integer"
                Case "SMALLINT"
                    Return "Short"
                Case "TINYINT"
                    Return "Byte"
                Case "FLOAT"
                    Return "Double"
                Case "REAL"
                    Return "Single"
                Case "NUMERIC"
                    Return "Decimal"
                Case "DECIMAL"
                    Return "Decimal"
                Case "MONEY"
                    Return "Decimal"
                Case "SMALLMONEY"
                    Return "Decimal"
                Case "DATETIME"
                    Return "Date"
                Case "TIMESTAMP"
                    Return "System.Data.Linq.Binary"
                Case "SMALLDATETIME"
                    Return "Date"
                Case Else
                    If Me.Length = 1 Then
                        Return "String"
                    Else
                        Return "String"
                    End If
            End Select
        End Get
    End Property

    Public ReadOnly Property DBMLDataDBType() As String
        Get
            Select Case mDataType.ToUpper
                Case "AUTONUMBER"
                    Return "Int NOT NULL IDENTITY"
                Case "UNIQUEIDENTIFIER"
                    Return "Int NOT NULL IDENTITY"
                Case "BIT"
                    Return "BIT"
                Case "VARBINARY"
                    Return "System.Data.Linq.Binary"
                Case "NCHAR"
                    Return "NChar(" & Me.Length & ")"
                Case "NVARCHAR"
                    Return "NVarChar(" & Me.Length & ")"
                Case "NTEXT"
                    Return "NText"
                Case "TEXT"
                    Return "Text"
                Case "CHAR"
                    Return "NVarChar(" & Me.Length & ")"
                Case "VARCHAR"
                    Return "VarChar(" & Me.Length & ")"
                Case "BIGINT"
                    Return "BigInt"
                Case "INT"
                    Return "Int"
                Case "SMALLINT"
                    Return "SmallInt"
                Case "TINYINT"
                    Return "TinyInt"
                Case "FLOAT"
                    Return "Float"
                Case "REAL"
                    Return "Real"
                Case "NUMERIC"
                    Return "Decimal(18,0)"
                Case "DECIMAL"
                    Return "Decimal(18,0)"
                Case "MONEY"
                    Return "Money"
                Case "SMALLMONEY"
                    Return "SmallMoney"
                Case "DATETIME"
                    Return "DateTime"
                Case "TIMESTAMP"
                    Return "rowversion"
                Case "SMALLDATETIME"
                    Return "SmallDateTime"
                Case Else
                    If Me.Length = 1 Then
                        Return "System.Char"
                    Else
                        Return "System.String"
                    End If
            End Select
        End Get
    End Property

    Public ReadOnly Property CanBeNull() As String
        Get
            If IsNullable Then
                Return "true"
            Else
                Return "false"
            End If
        End Get
    End Property

    Public ReadOnly Property IsNullable() As Boolean
        Get
            Select Case mDataType.ToUpper
                Case "AUTONUMBER"
                    Return False
                Case "BIT"
                    Return Me.AllowNulls
                Case "UNIQUEIDENTIFIER"
                    Return True
                Case "VARBINARY"
                    Return False
                Case "NCHAR"
                    If Me.Length = 1 Then
                        Return Me.AllowNulls
                    Else
                        Return False
                    End If
                Case "NVARCHAR"
                    If Me.Length = 1 Then
                        Return Me.AllowNulls
                    Else
                        Return False
                    End If
                Case "NTEXT"
                    Return False
                Case "TEXT"
                    Return False
                Case "CHAR"
                    If Me.Length = 1 Then
                        Return Me.AllowNulls
                    Else
                        Return False
                    End If
                Case "VARCHAR"
                    If Me.Length = 1 Then
                        Return Me.AllowNulls
                    Else
                        Return False
                    End If
                Case "BIGINT"
                    Return Me.AllowNulls
                Case "INT"
                    Return Me.AllowNulls
                Case "SMALLINT"
                    Return Me.AllowNulls
                Case "TINYINT"
                    Return Me.AllowNulls
                Case "FLOAT"
                    Return Me.AllowNulls
                Case "REAL"
                    Return Me.AllowNulls
                Case "NUMERIC"
                    Return Me.AllowNulls
                Case "DECIMAL"
                    Return Me.AllowNulls
                Case "MONEY"
                    Return Me.AllowNulls
                Case "SMALLMONEY"
                    Return Me.AllowNulls
                Case "DATETIME"
                    Return Me.AllowNulls
                Case "TIMESTAMP"
                    Return False
                Case "SMALLDATETIME"
                    Return True
                Case Else
                    If Me.Length = 1 Then
                        Return Me.AllowNulls
                    Else
                        Return Me.AllowNulls
                    End If
            End Select
        End Get
    End Property

    Public ReadOnly Property DBMLDataType() As String
        Get
            Select Case mDataType.ToUpper
                Case "AUTONUMBER"
                    Return "System.Int32"
                Case "UNIQUEIDENTIFIER"
                    Return "System.Guid"
                Case "VARBINARY"
                    Return "System.Data.Linq.Binary"
                Case "NCHAR", "NVARCHAR", "NTEXT", "TEXT", "CHAR", "VARCHAR"
                    If Me.Length = 1 Then
                        Return "System.Char"
                    Else
                        Return "System.String"
                    End If
                Case "BIGINT"
                    Return "System.Int64"
                Case "INT"
                    Return "System.Int32"
                Case "SMALLINT"
                    Return "System.Int16"
                Case "TINYINT"
                    Return "System.Byte"
                Case "FLOAT"
                    Return "System.Double"
                Case "REAL"
                    Return "System.Single"
                Case "NUMERIC"
                    Return "System.Decimal"
                Case "DECIMAL"
                    Return "System.Decimal"
                Case "MONEY"
                    Return "System.Decimal"
                Case "SMALLMONEY"
                    Return "System.Decimal"
                Case "DATETIME"
                    Return "System.DateTime"
                Case "TIMESTAMP"
                    Return "System.Data.Linq.Binary"
                Case "SMALLDATETIME"
                    Return "System.DateTime"
                Case Else
                    If Me.Length = 1 Then
                        Return "System.Char"
                    Else
                        Return "System.String"
                    End If

            End Select
        End Get
    End Property

    Public ReadOnly Property DBMLClassDeclaration()
        Get
            If IsNullable Then
                Return "System.Nullable(Of " & DBMLDataClassName & ")"
            Else
                Return DBMLDataClassName
            End If

        End Get
    End Property

End Class
